const express = require("express");
const log = require("../model/log");
const user = require("../model/user");
const logcomment = require("../model/logcoment");
const router = express.Router();

// 日志发表
router.post("/published", (req, res) => {

    log.create({
        author: req.session.user._id,
        content: req.body.content,
        date: req.body.date
    }).then(result => {
        if (result) {
            res.send({
                code: 1,
                message: "日志发表成功"
            })
        } else {
            res.send({
                code: 0,
                message: "日志发表失败，请稍后再试"
            })
        }
    })
})

// 个人日志渲染
router.get("/personalspace", (req, res) => {
    if (req.session.ifLogin) {
        user.findOne({ _id: req.session.user._id }).then(result => {
            user.findOne({ isAdmin: true }).then(admin => {
                log.find({ author: admin._id }).count().then(total => {
                   
                    res.render("personalspace", {
                        iflogin: req.session.ifLogin,
                        user: result,
                        total,
                        admin
                    });
                })
            })
        })
    } else {
        user.findOne({ isAdmin: true }).then(admin => {
            log.find({ author: admin._id }).count().then(total => {
                res.render("personalspace", {
                    iflogin: req.session.ifLogin,
                    total,
                    admin
                });
            })
        })
    }

})


// 获取个人日志数据
router.post("/personalspace", (req, res) => {
    user.findOne({ isAdmin: true }).then(admin => {
        log
            .find({ author: admin._id })
            .sort({ date: -1 })
            .populate(["author", "logcom"])
            .then(art => {
                res.send({
                    art
                })
            })
    })
})

// 日志圈渲染
router.get("/logcircle", (req, res) => {
    if (req.session.ifLogin) {
        user.findOne({ _id: req.session.user._id }).then(result => {
            log.find({ author: req.session.user._id }).count().then(total => {
                res.render("space", {
                    iflogin: req.session.ifLogin,
                    user: result,
                    total
                });
            })
        })
    } else {
        res.render("space", {
            iflogin: req.session.ifLogin
        });
    }
})
// 获取日志圈数据
router.post("/logcircle", (req, res) => {
    log
        .find()
        .sort({ date: -1 })
        .populate(["author", "logcom"])
        .then(art => {
            res.send({
                art
            })
        })
})

// 删除日志
router.get("/delete", (req, res) => {
    log.deleteOne({ _id: req.query.id }).then(result => {
        logcomment.deleteMany({log:req.query.id}).then(com=>{
            if (result.deletedCount) {
                res.send({
                    code: 1,
                    message: "日志删除成功"
                })
            } else {
                res.send({
                    code: 0,
                    message: "日志删除失败"
                })
            }
        })
    })
})



module.exports = router;
